package com.attendance.service.impl;

import com.attendance.common.CommonResult;
import com.attendance.mapper.LeaveRecordMapper;
import com.attendance.pojo.LeaveRecord;
import com.attendance.pojo.Pages;
import com.attendance.service.LeaveService;
import com.attendance.vo.LeaveRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LeaveServiceImpl implements LeaveService {

    @Autowired
    private LeaveRecordMapper leaveRecordMapper;

    public CommonResult find(LeaveRecordVo leaveRecordVo, Pages pages){
        PageHelper.startPage(pages.getPage(),pages.getNum());
        return CommonResult.success(leaveRecordMapper.find(leaveRecordVo));
    }

    public CommonResult add(LeaveRecord leaveRecord){
        if(leaveRecordMapper.insert(leaveRecord)==1){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    public CommonResult update(LeaveRecord leaveRecord){
        UpdateWrapper<LeaveRecord> warp = new UpdateWrapper<>();
        if (leaveRecord.getLeaveReason()!=null && !leaveRecord.getLeaveReason().equals("")){
            warp.set("leaveReason",leaveRecord.getLeaveReason())
                    .set("leaveStatus",leaveRecord.getLeaveStatus())
                    .eq("leaveId",leaveRecord.getLeaveId());
        }else {
            warp.set("leaveBeginTime",leaveRecord.getLeaveBeginTime())
                    .set("leaveEndTime",leaveRecord.getLeaveEndTime())
                    .set("leaveTitle",leaveRecord.getLeaveTitle())
                    .set("leaveResult",leaveRecord.getLeaveResult())
                    .set("leaveStatus",leaveRecord.getLeaveStatus())
                    .set("result",leaveRecord.getResult()).eq("leaveId",leaveRecord.getLeaveId());
        }
        if (leaveRecordMapper.update(leaveRecord,warp)==1){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    public CommonResult delete(LeaveRecord leaveRecord){
        QueryWrapper<LeaveRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("leaveId",leaveRecord.getLeaveId());
        if (leaveRecordMapper.delete(queryWrapper)==1){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }
}
